#include <bits/stdc++.h>
using namespace std;
int n;
string s;
bool spe[30];
bool isspecial(char c) {
  int x = c - 'a';
  return spe[x];
}
int main() {
  int tc;
  cin >> tc;
  while (tc--) {
    cin >> n >> s;
    int chs;
    memset(spe, 0, sizeof(spe));
    cin >> chs;
    while (chs--) {
      char c;
      cin >> c;
      spe[c - 'a'] = 1;
    }
    int ans = 0, cnt = 0;
    for (int i = 1; i < n; ++i) {
      char c = s[i];
      cnt++;
      if (isspecial(c)) {
        ans = max(ans, cnt);
        cnt = 0;
      }
    }
    cout << ans << endl;
  }
}
